home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / groffvar.zoo / source / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-14  |  3.2 KB  |  100 lines

  1. /*
  2. From: gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>)
  3. Newsgroups: net.sources
  4. Subject: getopt library routine
  5. Date: 30 Mar 85 04:45:33 GMT
  6. */
  7.  
  8. /*
  9.   getopt -- public domain version of standard System V routine
  10.  
  11.   Strictly enforces the System V Command Syntax Standard;
  12.   provided by D A Gwyn of BRL for generic ANSI C implementations
  13. */
  14.  
  15. #include  <compiler.h>
  16. #include  <stddef.h>
  17. #include  <stdio.h>
  18. #include  <string.h>
  19. #include  <unistd.h>
  20.  
  21. int opterr = 1;                 /* error => print message  */
  22. int optind = 1;                 /* next argv[] index       */
  23. char *optarg = NULL;            /* option parameter if any */
  24.  
  25. static int Err __PROTO((char *, char *, int));
  26.  
  27. static int Err(name, mess, c)   /* returns '?'             */
  28. char *name;                     /* program name argv[0]    */
  29. char *mess;                     /* specific message        */
  30. int c;                          /* defective option letter */
  31. {
  32.   if (opterr)
  33.     fprintf(stderr, "%s: %s -- %c\n", name, mess, c);
  34.  
  35.   return '?';                   /* erroneous-option marker */
  36. }
  37.  
  38.  
  39. int getopt(argc, argv, optstring) /* returns letter, '?', EOF  */
  40. int argc;                         /* argument count from main  */
  41. char *const *argv;                /* argument vector from main */
  42. const char *optstring;            /* allowed args, e.g. "ab:c" */
  43. {
  44.   static int sp = 1;              /* position within argument  */
  45.   register int osp;               /* saved `sp' for param test */
  46.   register int c;                 /* option letter             */
  47.   register char *cp;              /* -> option in `optstring'  */
  48.  
  49.   optarg = NULL;
  50.  
  51.   if (sp == 1)                    /* fresh argument            */
  52.     if (optind >= argc            /* no more arguments         */
  53.         || argv[optind][0] != '-'   /* no more options         */
  54.         || argv[optind][1] == '\0') /* not option; stdin       */
  55.       return EOF;
  56.     else if (strcmp(argv[optind], "--") == 0)
  57.     {
  58.       ++optind;                   /* skip over "--"            */
  59.       return EOF;                 /* "--" marks end of options */
  60.     }
  61.  
  62.   c = argv[optind][sp];           /* option letter             */
  63.   osp = sp++;                     /* get ready for next letter */
  64.  
  65.   if (argv[optind][sp] == '\0')   /* end of argument           */
  66.   {
  67.     ++optind;                     /* get ready for next try    */
  68.     sp = 1;                       /* beginning of next argument */
  69.   }
  70.  
  71.   if (c == ':'                    /* optstring syntax conflict */
  72.       || (cp = strchr(optstring, c)) == NULL)     /* not found */
  73.     return Err(argv[0], "illegal option", c);
  74.  
  75.   if (cp[1] == ':')               /* option takes parameter    */
  76.   {
  77.     if (osp != 1)
  78.       return Err(argv[0], "option must not be clustered", c);
  79.  
  80.     if (sp == 1)
  81.     {
  82.       if (optind >= argc)
  83.         return Err(argv[0], "option requires an argument", c);
  84.       else
  85.       {
  86.         optarg = (char *) argv[optind]; /* make parameter available */
  87.         ++optind;                       /* skip over parameter */
  88.       }
  89.     }
  90.     else /* parameter followed by argument */
  91.     {
  92.       optarg = argv[optind] + 2; /* skip -X */
  93.       sp = 1;
  94.       ++optind;
  95.     }
  96.   }
  97.  
  98.   return c;
  99. }
  100.